eslint-factory: broaden no-unsafe-catch-error-property to flag .status, .cause, .name - #42239
Conversation
…e/name with typeof guard Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR updates the eslint-factory “unsafe catch error property” rules to catch additional common error-property accesses (notably HTTP-style .status) and adds an additional guard pattern (typeof err === 'object') to reduce false positives at real call sites.
Changes:
- Expanded both rules’
UNSAFE_PROPERTIESto includestatus,cause, andname. - Added recognition of
typeof <errVar> === 'object'(and the reversed literal form) as an accepted guard in both rules. - Added RuleTester coverage for the new properties and the new
typeofguard behavior.
Show a summary per file
| File | Description |
|---|---|
| eslint-factory/src/rules/no-unsafe-promise-catch-error-property.ts | Expands unsafe properties and adds typeof … === 'object' guard detection for .catch() callbacks. |
| eslint-factory/src/rules/no-unsafe-promise-catch-error-property.test.ts | Adds new valid/invalid cases for .status, .cause, .name, including typeof guard variants. |
| eslint-factory/src/rules/no-unsafe-catch-error-property.ts | Expands unsafe properties and adds typeof … === 'object' guard detection for catch (err) blocks. |
| eslint-factory/src/rules/no-unsafe-catch-error-property.test.ts | Adds new valid/invalid cases for .status, .cause, .name, and typeof guard variants (including suggestion expectations). |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 4/4 changed files
- Comments generated: 5
- Review effort level: Low
| hasSuggestions: true, | ||
| docs: { | ||
| description: "Disallow direct access to .message, .stack, or .code on a caught error variable without a getErrorMessage guard", | ||
| description: "Disallow direct access to .message, .stack, .code, .status, .cause, or .name on a caught error variable without a getErrorMessage guard", |
| hasSuggestions: true, | ||
| docs: { | ||
| description: "Disallow direct access to .message, .stack, or .code on a promise .catch() callback parameter without a getErrorMessage guard", | ||
| description: "Disallow direct access to .message, .stack, .code, .status, .cause, or .name on a promise .catch() callback parameter without a getErrorMessage guard", |
| // typeof varName === 'object' or 'object' === typeof varName | ||
| if (node.operator === "===") { | ||
| const { left, right } = node; | ||
| const isTypeofObject = | ||
| (left.type === AST_NODE_TYPES.UnaryExpression && |
| // typeof varName === 'object' or 'object' === typeof varName | ||
| if (node.operator === "===") { | ||
| const { left, right } = node; | ||
| const isTypeofObject = | ||
| (left.type === AST_NODE_TYPES.UnaryExpression && |
| messageId: "wrapWithInstanceof", | ||
| data: { errorVar: "err", prop: "status" }, | ||
| output: `try { f(); } catch (err) { if ((err instanceof Error ? err.status : undefined) === 404) { } }`, |
🤖 PR Triage — §28376613466
Score breakdown: Impact 10 + Urgency 5 + Quality 5 Rationale: Broadens the
|
|
🎉 This pull request is included in a new release. Release: |
no-unsafe-catch-error-property(and its promise variant) only flagged.message,.stack,.codeon unguarded caught error variables — missing.statusaccesses likeif (err.status === 404)that are common in HTTP error handling.Changes
UNSAFE_PROPERTIESin bothno-unsafe-catch-error-propertyandno-unsafe-promise-catch-error-propertyto includestatus,cause, andnametypeof err === 'object'as an accepted guard (strict===only; alongside existinginstanceof ErrorandgetErrorMessageguards) — real call sites that checktypeof err === 'object'before reading.statusare no longer false positivestypeofguard variants (valid)